package com.xts.weinfo.presenter;

import com.xts.weinfo.R;
import com.xts.weinfo.base.BaseApp;
import com.xts.weinfo.base.BasePresenter;
import com.xts.weinfo.base.Constants;
import com.xts.weinfo.bean.LoginBean;
import com.xts.weinfo.bean.RegisterBean;
import com.xts.weinfo.model.RegisterModel;
import com.xts.weinfo.net.LoginService;
import com.xts.weinfo.net.ResultCallBack;
import com.xts.weinfo.util.SpUtil;
import com.xts.weinfo.view.RegisterView;

public class RegisterPresenter extends BasePresenter<RegisterView> {

    private RegisterModel mRegisterModel;
    private String registerSuccess = BaseApp.getRes().getString(R.string.register_success);

    @Override
    protected void initModel() {
        mRegisterModel = new RegisterModel();
        //让父类在界面销毁的时候通知M层取消网络和订阅关系
        addModel(mRegisterModel);
    }

    //用户名密码注册
    public void register(String name, String psd) {
       /* mRegisterModel.register(name, psd, new ResultCallBack<RegisterBean>() {
            @Override
            public void onSuccess(RegisterBean registerBean) {
                if (registerSuccess.equals(registerBean.code)) {
                    //注册成功
                    mView.registerSuccess();
                } else {
                    //注册失败
                }
                mView.showToast(registerBean.code);
            }

            @Override
            public void onFail(String msg) {

            }
        });*/

       //注册环信账号
       mRegisterModel.registerEaseMob(name, psd, new ResultCallBack<String>() {
           @Override
           public void onSuccess(String s) {
                mView.showToast(s);
                mView.registerSuccess();
           }

           @Override
           public void onFail(String msg) {
                mView.showToast(msg);
           }
       });
    }

    //三方注册+三方登录
    public void registerAccess(String name, String psd, String uid, String typeId) {
        mRegisterModel.registerAccess(uid,typeId,name, psd, new ResultCallBack<LoginBean>() {
            @Override
            public void onSuccess(LoginBean loginBean) {
                if (LoginService.LOGIN_SUCCESS_CODE.equals(loginBean.getCode())){
                    //成功
                    mView.loginSuccess();
                    //保存用户信息
                    saveUserData(loginBean);
                }else {
                    //一般是不会失败的
                    mView.showToast(BaseApp.getRes().getString(R.string.login_fail));
                }
            }

            @Override
            public void onFail(String msg) {
                mView.showToast(msg);
            }
        });
    }

    ////保存用户信息
    private void saveUserData(LoginBean loginBean) {
        LoginBean.UserBean user = loginBean.getUser();
        String token = loginBean.getToken();

        SpUtil.setParam(Constants.TOKEN,token);
        SpUtil.setParam(Constants.USERNAME,user.getName());
        SpUtil.setParam(Constants.USERID,user.getUserid());
        SpUtil.setParam(Constants.PHOTO,user.getHeaderpic());
    }
}
